﻿/*
 * @author: wizardc
 */

using System;
using System.IO;
using MessagePack;

namespace DouEditor.Utils
{
    /// <summary>
    /// MessagePack 工具类
    /// </summary>
    public static class MessagePackUtil
    {
        [Obsolete("不建议使用MessagePack的Json方法，可以序列化但是反序列化时会因为缺少类信息而报错")]
        public static T LoadJsonData<T>(string path) {
            var txt = File.ReadAllText(path);
            var json = MessagePackSerializer.ConvertFromJson(txt);
            return MessagePackSerializer.Deserialize<T>(json);
        }
        
        [Obsolete("不建议使用MessagePack的Json方法，可以序列化但是反序列化时会因为缺少类信息而报错")]
        public static void SaveJsonData<T>(string path, T data) {
            EditorUtil.CreateDirByFilePath(path);
            var json = MessagePackSerializer.SerializeToJson(data);
            EditorUtil.SaveTxtFile(path, json);
        }
        
        public static T LoadData<T>(string path) {
            var bytes = File.ReadAllBytes(path);
            return MessagePackSerializer.Deserialize<T>(bytes);
        }
        
        public static void SaveData<T>(string path, T data) {
            EditorUtil.CreateDirByFilePath(path);
            var bytes = MessagePackSerializer.Serialize(data);
            File.WriteAllBytes(path, bytes);
        }
    }
}
